<?php
class weebUIAllocineItem
{
    /**
    * Loads details
    **/
    function load()
    {
        if ( $this->id == null ) throw new weebUIMissingDataException( 'weebUIAllocineItem::id is not set' );

        $movieData = array();
        $detailsPage = utf8_encode( file_get_contents( 'http://www.allocine.fr/film/fichefilm_gen_cfilm=' . $this->id . '.html' ) );

        // title
        if ( preg_match( '|<td><h1 style="color: #D20000"><b>(.*?)</b></h1></td>|', $detailsPage, $matches ) )
        {
            $this->title = utf8_decode( $matches[1] );
        }

        // original title
        if ( preg_match( '|<h4>Titre original : <i>(.*?)</i></h4>|', $detailsPage, $matches ) )
        {
            $this->originalTitle = utf8_decode( $matches[1] );
        }

        // genres
        $substring = substr( $detailsPage, strpos( $detailsPage, '<h4>Genre ' ) + 12 );
        $substring = substr( $substring, 0, strpos( $substring, '</h4>' ) );
        if ( preg_match_all( '#<a[^>]*href="[^"]+"[^>]*>(.*?)</a>#', $substring, $matches, PREG_SET_ORDER ) )
        {
            foreach ( $matches as $match )
            {
                $genres[] = $match[1];
            }
            $this->genres = $genres;
        }

        // release date
        if ( preg_match( '|<h4>Date de sortie : <b>(.*?)</b>|', $detailsPage, $matches ) )
        {
            $this->releaseDate = new AllocineItemDate( utf8_decode( $matches[1] ) );
        }

        // director(s)
        $substring = substr( $detailsPage, strpos( $detailsPage, '<h4>Réalisé par ' ) + 17 );
        $substring = substr( $substring, 0, strpos( $substring, '</h4>' ) );
        if ( preg_match_all( '#<a[^>]*href="([^"]+)"[^>]*>(.*?)</a>#', $substring, $matches, PREG_SET_ORDER ) )
        {
            foreach ( $matches as $match )
            {
                $directors[] = array( 'name' => utf8_decode( $match[2] ), 'url' => $match[1] );
            }
            $this->directors = new AllocineItemPersonList( $directors );
        }

        // distributed by...
        $substring = substr( $detailsPage, strpos( $detailsPage, '<h4>Distribué par ' ) + 19 );
        $substring = substr( $substring, 0, strpos( $substring, '</h4>' ) );
        if ( preg_match_all( '#<a[^>]*href="([^"]+)"[^>]*>(.*?)</a>#', $substring, $matches, PREG_SET_ORDER ) )
        {
            foreach ( $matches as $match )
            {
                $distributedBy[] = array( 'name' => utf8_decode( $match[2] ), 'url' => $match[1] );
            }
            $this->distributedBy = new AllocineItemPersonList( $distributedBy );
        }

        // runtime
        $substring = substr( $detailsPage, strpos( $detailsPage, '<h4>Durée : ' ) + 13 );
        $substring = substr( $substring, 0, strpos( $substring, '&nbsp;</h4>' ) );
        sscanf( $substring, '%dh %dmin', $hours, $minutes );
        $this->runtime = $hours * 60 + $minutes;

        // synopsis
        $substring = substr( $detailsPage, strpos( $detailsPage, '<h3><b>Synopsis</b></h3>' ) + 25 );
        $substring = substr( $substring, strpos( $substring, '<h4>' ) + 4 );
        $substring = substr( $substring, 0, strpos( $substring, '</h4>' ) );
        $this->synopsis = strip_tags( utf8_decode( $substring ) );

        // cover
        $imagesPage = utf8_encode( file_get_contents( 'http://www.allocine.fr/film/galerievignette_gen_cfilm=' . $this->id . '.html' ) );
        if ( preg_match( '#<img id=\'imgNormal\' class=\'photo\' src=\'([^\']+)\'#', $imagesPage, $matches ) )
        {
            $this->cover = $matches[1];
        }

        // casting
        $castingPage = file_get_contents( 'http://www.allocine.fr/film/casting_gen_cfilm=' . $this->id . '.html' );
        $substring = substr( $castingPage, strpos( $castingPage, '<b>Acteurs</b></h4><hr /></td></tr>' ) );
        $substring = substr( $substring, 0, strpos( $substring, '<h4' ) );
        if ( preg_match_all( '#<h5>(.*?)</h5>.*?<h5><a href="(/personne/fichepersonne_gen_cpersonne=([0-9]+)\.html)" class="link1">(.*?)</a>.*?</h5>#s', $substring, $matches, PREG_SET_ORDER ) )
        {
            foreach ( $matches as $match )
            {
                $actors[] = array( 'name' => $match[4], 'url' => $match[2] );
                $actorsRoles[] = array( 'name' => $match[1], 'url' => '' );
            }
            $this->actors = new AllocineItemPersonList( $actors );
            $this->actorsRoles = new AllocineItemPersonList( $actorsRoles );
        }
    }

    var $id, $by, $year;
    var $title, $originalTitle, $directors, $distributedBy, $runtime, $synopsis, $cover, $releaseDate, $genres;
    var $actors, $actorsRoles;
}

class AllocineItemDate
{
    function __construct( $dateString )
    {
        list( $this->day, $month, $this->year ) = explode( ' ', $dateString );
        switch ( strtolower( $month ) )
        {
            case 'janvier':   $this->month = 1;  break;
            case 'février':   $this->month = 2;  break;
            case 'mars':      $this->month = 3;  break;
            case 'avril':     $this->month = 4;  break;
            case 'mai':       $this->month = 5;  break;
            case 'juin':      $this->month = 6;  break;
            case 'juillet':   $this->month = 7;  break;
            case 'août':      $this->month = 8;  break;
            case 'septembre': $this->month = 9;  break;
            case 'octobre':   $this->month = 10; break;
            case 'novembre':  $this->month = 11; break;
            case 'décembre':  $this->month = 12; break;
        }
    }

    function __toString()
    {
        return $this->year;
    }

    public $day, $month, $year;
}

class AllocineItemPersonList
{
    function __construct( $personList )
    {
        $this->personList = $personList;
    }

    function __toString()
    {
        $names = array();
        foreach ( $this->personList as $person )
        {
            $names[] = $person['name'];
        }
        return '|' . implode( '|', $names ) . '|';
    }
    public $personList = array();
}
?>